<template>
	<view class="container">
		<view class="pro_det-head u-flex u-padding-40 z-b-r white-bg u-font-32">
			<view class="u-flex-1">{{projectDetailData.project_name}}</view>
			<view class="u-margin-left-20" style="color: #007AFF;" @click="reloadProjectDetail">刷新<u-icon label-size="32" name="reload"></u-icon></view>
		</view>
	
		<view class="pro_det-body u-margin-top-20 white-bg u-padding-40 z-b-r">
			<view class="pro_item">
				<text>项目经理姓名：</text>
				<text>{{ projectDetailData.pm_name }}</text>
			</view>
			<view class="pro_item">
				<text>项目经理电话：</text>
				<text>{{ projectDetailData.pm_phone }}</text>
			</view>
			<view class="pro_item">
				<text>项目地址：</text>
				<text>{{ projectDetailData.project_address }}</text>
			</view>
			<!-- <view class="pro_item">
				<text>钻机编号：</text>
				<text>{{ projectDetailData.drill_id.join() || '请分配钻机' }}</text>
			</view> -->
			<view class="pro_item" v-if="projectDetailData.project_begin_date">
				<text>项目开启时间：</text>
				<text>{{ projectDetailData.project_begin_date }}</text>
			</view>
			<view class="pro_item" v-else>
				项目开启时间：<u-tag text="未开启" type="info" size="mini" shape="circle"/>
			</view>
			<view class="pro_item" v-if="projectDetailData.project_end_date">
				<text>项目结束时间：</text>
				<text>{{ projectDetailData.project_end_date }}</text>
			</view>
			<view class="pro_item" v-else>
				项目结束时间：<u-tag text="施工中" type="warning" size="mini" shape="circle"/>
			</view>
		</view>
		<view class="pro_det-body u-margin-top-20 white-bg u-padding-40 z-b-r">
			<template v-if="projectDetailData.project_status !== 3">
				<u-cell-group>
					<u-cell-item :title="proChecked ? '关闭项目' : '启动项目'" :arrow="false" icon="clock">
						<u-switch slot="right-icon" size="40" @change="switchChange" v-model="proChecked"></u-switch>
					</u-cell-item>
					<u-cell-item @click="projectLink('../allot-drill/allot-drill')" icon="setting-fill" title="分配钻机"></u-cell-item>
					<u-cell-item @click="projectLink(`../allot-worker/allot-worker?salaryWay=${curProjectSalaryWay}&projectID=${curProjectID}`)" icon="man-add" title="分配员工"></u-cell-item>
					<u-cell-item @click="projectLink('../drillBindWorker/drillBindWorker')" icon="hourglass" title="钻机绑定员工"></u-cell-item>
					<u-cell-item @click="projectLink('../upload-expend/upload-expend')" icon="arrow-upward" title="上传支出"></u-cell-item>
					<u-cell-item @click="projectLink('../add-income/add-income')" icon="plus" title="添加收入"></u-cell-item>
					
				</u-cell-group>
			</template>
			<template v-if="projectDetailData.project_status === 3">
				<u-empty text="项目已结束" mode="data"></u-empty>
			</template>
		</view>
		<view class="u-margin-top-20 u-margin-bottom-20 white-bg z-b-r u-padding-20">
			<view class="pro_item">
				<u-cell-item @click="updataSalaryWay" :title="`工资结算方式：${projectDetailData.salary_way || ''}`" arrow-direction="down"></u-cell-item>
				<u-select v-model="salaryWayShow" mode="single-column" @confirm="salaryWayConfirm" :list="salaryWays"></u-select>
			</view>
			<view class="pro_item">
				<u-cell-item icon="edit-pen" @click="projectLink('../edit-salary-standard/edit-salary-standard')" title="修改工资标准"></u-cell-item>
			</view>
			<view class="pro_item">
				<u-cell-item icon="edit-pen" @click="projectLink('../update-hole-status/update-hole-status')" title="管理孔号状态"></u-cell-item>
			</view>
		</view>
		<view class="pro_det-body u-margin-top-20 white-bg u-padding-20 z-b-r">
			<view class="pro_item">
				<u-cell-item icon="file-text" @click="projectLink('../worker-detail/worker-detail')" title="员工资料"></u-cell-item>
			</view>
			<view class="pro_item">
				<u-cell-item icon="rmb-circle" @click="projectLink('../project-expend/project-expend')" title="项目支出"></u-cell-item>
			</view>
			<view class="pro_item">
				<u-cell-item icon="rmb-circle" @click="projectLink('../project-income/project-income')" title="项目收入"></u-cell-item>
			</view>
			<view class="pro_item">
				<u-cell-item icon="clock" @click="projectLink('../worker-job-info/worker-job-info')" title="工作量信息"></u-cell-item>
			</view>
		</view>
		<!-- 模态框 -->
		<u-modal v-model="modalShow" @cancel="cancelEvent" @confirm="confirmEvent" show-cancel-button="true" :content="modalCon"></u-modal>
		<!-- 消息提示 -->
		<u-toast ref="uToast" />
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {mapState} from "vuex";
	import {myTime} from "@/common/formatTime.js";
	export default {
		data() {
			return {
				projectDetailData:{},
				showPopup:false,
				proChecked:false,
				modalShow:false,
				modalCon:"请确认项目是否已经结束",
				curProjectID:"",
				curProjectSalaryWay:"",
				salaryWayShow:false,
				salaryWays:[
					{
						value: 'c1',
						label: '按尺结算'
					},
					{
						value: 't2',
						label: '按天数结算'
					}
				]
			}
		},
		onLoad(params) {
			this.curProjectID = params.projectID;
			this.curProjectSalaryWay = params.salaryWay;
			this.getProjectDetail(params.projectID)
		},
		computed:{
			...mapState({
				token:"token"
			})
		},
		methods: {
			updataSalaryWay(){
				this.salaryWayShow = true;
			},
			async salaryWayConfirm(params){
				let {token,curProjectID} = this;
				let data = {
						token,
						id:curProjectID,
						type:"project",
						salary_way:params[0].label
					}
				this.$loading("正在修改");
				let res = await this.$network({
					api:"main",
					data:{
						action:"projectAdmin/updateProjectSalaryWay",
						data
					}
				})
				
				uni.hideLoading()
				if(res.status === 0){
					this.projectDetailData.salary_way = params[0].label;
					this.$refs.uTips.show({
						title: '修改成功',
						type: 'success',
					})
				}
			},
			async getProjectDetail(id){
				this.$loading("正在加载")
				let res = await this.$network({
					api:"main",
					data:{
						action:"projectAdmin/getProjectDetail",
						data:{id}
					}
				})
				uni.hideLoading()
				if(res.status === 0){
					this.projectDetailData = res.data[0];
					//检测项目是否已经启动
					if(this.projectDetailData.project_status === 1){
						this.proChecked = false;
					} else if(this.projectDetailData.project_status === 2){
						this.proChecked = true;
					}
				}
			},
			switchChange(val){
				if(!val){
					this.modalCon = "请确认项目是否已经结束"
				} else if(val){
					this.modalCon = "请确认项目是否准备完毕"
				}
				this.modalShow = true;
			},
			cancelEvent(){
				this.proChecked = !this.proChecked;
			},
			async confirmEvent(){
				// 确认 关闭或者开启项目   保存状态  设定开启时间
				
				// 1、确认开启检测是否已经分配员工 和 钻机
				
				// console.log(this.projectDetailData);
				let proStatus = this.projectDetailData.project_status,
					res,
					toastText="";
				let {token} = this;	
				this.$loading("正在处理");
				this.showPopup = false;
				switch (proStatus){
					case 1: //需要开启 状态改为2
						proStatus = 2;
						toastText = "项目启动成功";
						break;
					case 2: //需要结束 状态改为3
						proStatus = 3;
						toastText = "项目已关闭"
						break;
				}
				
				// 员工恢复空闲状态，但是工作记录要保存
				let {projectDetailData} = this;
				let updateWKstatusTasks = [];
				projectDetailData.worker_ids.forEach((item,index)=>{
					updateWKstatusTasks.push(this.$network({
						api:"main",
						data:{
							action:"allotWKAdmin/updateWorkerStatus",
							data:{
								status:0,
								wkID:item.wkID,
								token
							}
						}
					}))
				})
				
				let updateWKstatusRes = await Promise.all(updateWKstatusTasks);
				
				// 更改项目状态
				// console.log(proStatus,this.projectDetailData._id);
				let projectDate = myTime(Date.now())
				res = await this.$network({
					api:"main",
					data:{
						action:"projectAdmin/updateProjectStatus",
						data:{
							proStatus,
							id:this.projectDetailData._id,
							token,
							projectDate
						}
					}
				})
				
				if(res.status === 0){
					uni.hideLoading();
					this.$refs.uTips.show({
						title: toastText,
						type: 'success',
					})
				}
			},
			projectLink(path){
				let url = '';
				this.projectDetailData.drill_id ? url = `${path}?projectID=${this.projectDetailData._id}&drillID=${this.projectDetailData.drill_id}` : 
				url = `${path}?projectID=${this.projectDetailData._id}`;
				uni.navigateTo({url})
			},
			// 重新加载数据
			reloadProjectDetail(){
				this.getProjectDetail(this.curProjectID)
			}
		}
	}
</script>

<style>
	.pro_item{
		margin-bottom: 20rpx;
	}
</style>
